package games ;

import box2D.collision.shapes.B2CircleShape;
import box2D.collision.shapes.B2PolygonShape;
import box2D.collision.shapes.B2Shape;
import box2D.common.math.B2Vec2;
import box2D.dynamics.B2BodyDef;
import box2D.dynamics.B2DebugDraw;
import box2D.dynamics.B2FixtureDef;
import box2D.dynamics.B2World;
import com.funkyfurrymonkey.graphs.SquareProps;
import data.GlobalData;
import flash.display.BitmapData;
import haxe.Timer;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import box2D.dynamics.B2Body;
import box2D.dynamics.B2Fixture;
import openfl.events.Event;
import openfl.geom.Point;
import openfl.text.TextField;
import world2d.Avatar;

import world2d.Bullet2D;
import world2d.Cannon;
import world2d.ContactListener;
import objects2d.Ground2D;
import objects2d.Brick2D;

import com.funkyfurrymonkey.graphs.CreateBasics;
/**
 * ...
 * @author Marcos Martinez
 */
class VersusGame extends Sprite
{
		
	private var m_cGravity:B2Vec2;
	private var m_cWorld:B2World;
	private var m_cPhysicsDebug:Sprite;
	private var m_aBricks:Array<Brick2D>;
	
	public function new() 
	{
		super();		
		m_cPhysicsDebug = new Sprite();
		m_aBricks = new Array<Brick2D>();
	}
	
	public function init():Void
	{
		trace("reace on");
		m_cGravity = new B2Vec2(0, 10);
		m_cWorld = new B2World(m_cGravity, true);
		this.addChild(m_cPhysicsDebug);
		
		setDebugDraw();
		
		var m_cGround:Ground2D = new Ground2D(m_cWorld);
		m_cGround.create(0, 400, 800, 1);
	

		
		var bmd:BitmapData = new BitmapData(10, 10, true, 0x55000055);
		for ( i in 0...10)
		{
			var brick = new Brick2D(m_cWorld);
			brick.createBox(new Bitmap(bmd), 520, 360 - i * 20);
			this.addChild(brick.getCanvas());
			m_aBricks.push(brick);
		}
		
		var cProps = new SquareProps();
		cProps.m_nWidht = 100;
		cProps.m_nHeight = 50;
		cProps.m_nLineColor = 0xffff0000;
		this.addChild( new Bitmap(CreateBasics.Square( cProps)) );
		
		var cLeftPlayer:Avatar = new Avatar(m_cWorld);
		cLeftPlayer.createBox(20, 30, 50, 370);
		this.addChild(cLeftPlayer);

		var cRightPlayer:Avatar = new Avatar(m_cWorld);
		cRightPlayer.createBox(20, 30, 600, 370);
		this.addChild(cRightPlayer);
		
		var cContact:ContactListener = new ContactListener(cLeftPlayer);
		m_cWorld.setContactListener(cContact);
		
		//cTimer.run = movingWorld;
		GlobalData.m_cMainCanvas.stage.addEventListener(Event.ENTER_FRAME, movingWorld);
		
		var tf:TextField = new TextField();
		tf.textColor = 0xffffff;
		tf.text = "hitted : 0";
		this.addChild(tf);
		cLeftPlayer.onHitted = function(nCnt:Int)
		{
			tf.text = "hitted : " + nCnt; 
		};
		
	}
	
	private function movingWorld(ev:Event):Void
	{
		m_cWorld.step(0.025 , 10, 10);
		for (eachBrick in m_aBricks)
			eachBrick.updateTexture();
		m_cWorld.drawDebugData();
		m_cWorld.clearForces();
	}
	
	private function setDebugDraw():Void
	{
				// set debug draw
		var dbgDraw:B2DebugDraw = new B2DebugDraw();
		var dbgSprite:Sprite = new Sprite();
		addChild(dbgSprite);
		dbgDraw.setSprite(this);
		dbgDraw.setDrawScale(1 /GlobalData.PHYSIC_SCALE);
		dbgDraw.setFillAlpha(0.3);
		dbgDraw.setLineThickness(1.0);
		dbgDraw.setFlags(B2DebugDraw.e_shapeBit | B2DebugDraw.e_jointBit);
		m_cWorld.setDebugDraw(dbgDraw);
	}
}